window.onload = function () {
    showProductTable(1, "", "");        //显示商品列表
    brokenTotal();
    document.getElementById("delSelected").onclick = function () {
        var flag = false;
        var cbs = document.getElementsByName("pQR");
        for (var i = 0; i < cbs.length; i++) {
            if (cbs[i].checked) {
                flag = true;
                break;
            }
        }
        if (flag) {
            if (confirm("您确定要删除选中吗？")) {
                var kind = 1;
                for (var i = 0; i < cbs.length; i++) {
                    if (cbs[i].checked) {
                        product_del(cbs[i], cbs[i].value, kind);
                    }
                }
            }
        } else {
            alert("请选择要删除的记录!")
        }

    }
    document.getElementById("firstCb").onclick = function () {  //批量删除
        var cbs = document.getElementsByName("pQR");
        for (var i = 0; i < cbs.length; i++) {
            cbs[i].checked = this.checked;
        }
    }
};
var prePage = 1;

function changeAlign() {
    var t = document.getElementById("tbody");
    var x = t.getElementsByTagName("td");
    for (var i = 0; i < x.length; i++) {
        x[i].style = "text-align: center;vertical-align:middle;";
    }
}

function show(json, currentPage, totalPage) {
    console.log(json);
    var tb = document.getElementById("tbb");
    var length = tb.tBodies[0].rows.length;
    for (var i = 0; i < length; ++i) {
        tb.tBodies[0].deleteRow(0);//每次删除第一行
    }
    for (var i = 0; i < json.length; ++i) {
        var row = tb.tBodies[0].insertRow();
        row.insertCell().innerHTML = "<input type='checkbox' name='pQR' value=\"" + json[i].productQR + "\"/>";
        row.insertCell().innerText = i + 1;
        row.insertCell().innerText = json[i].productQR;
        row.insertCell().innerHTML = "<img width='50px' height='50px'  src=\"data:image/jpg;base64," + json[i].productPhoto + "\">";
        row.insertCell().innerText = json[i].productType;
        row.insertCell().innerText = json[i].productName;
        row.insertCell().innerText = json[i].productColor;
        row.insertCell().innerText = json[i].productNumber;
        row.insertCell().innerText = json[i].productDate;
        row.insertCell().innerText = json[i].productFactory;
        if (json[i].saleableInformation == 10) {      //出租
            row.insertCell().innerText = "出租";
            row.insertCell().innerText = json[i].rentPrice;//租价
            row.insertCell().innerHTML = "—";//售价
            row.insertCell().innerText = json[i].rentMoney;//押金
        }
        if (json[i].saleableInformation == 1) {       //出售
            row.insertCell().innerText = "出售";
            row.insertCell().innerHTML = "—";//租价
            row.insertCell().innerText = json[i].salePrice;//售价
            row.insertCell().innerHTML = "—";//押金
        }
        if (json[i].saleableInformation == 11) {      //租售
            row.insertCell().innerText = "租售";
            row.insertCell().innerText = json[i].rentPrice;//租价
            row.insertCell().innerText = json[i].salePrice;//售价
            row.insertCell().innerText = json[i].rentMoney;//押金
        }
        if (json[i].state == 0) {
            row.insertCell().innerText = "可用";
        }else {
            if (json[i].state == 1) {
                row.insertCell().innerText = "已租";
            }else {
                row.insertCell().innerText = "已售";
            }
        }
        if (json[i].isBroken == "0") {
            row.insertCell().innerText = "否";
        }else {
            row.insertCell().innerText = "是";
        }
        // 修改、删除功能图标
        row.insertCell().innerHTML = "<a style='text-decoration:none' class='ml-5' " +
            "onclick='product_edit(\"" + json[i].productQR + "\")' title='修改'>" +
            " <i class='Hui-iconfont'>&#xe6df;&nbsp;&nbsp;&nbsp;</i></a>" +
            "<a style='text-decoration:none' class='ml-5' " +
            "onclick='product_del(this,\"" + json[i].productQR + "\",0)' href='javascript:;' title='删除'>" +
            "<i class='Hui-iconfont'>&nbsp;&nbsp;&nbsp;&#xe6e2;</i></a>";
    }
    changeAlign();
    pageview(currentPage, totalPage);
    for (var i = 1; i <= totalPage; i++) {
        if (currentPage == i) {
            document.getElementById(currentPage).setAttribute("class", "active");
        } else {
            document.getElementById(i).removeAttribute("class", "active");
        }
    }
}

/*页码显示*/
function pageview(currentPage, totalPage) {
    var conType = document.getElementById("conType").value;
    var conName = document.getElementById("conName").value;
    var pages = document.getElementById("pages");
    for (var i = 1; i <= totalPage; i++) {
        var elem_li = document.createElement('li'); // 生成一个 li元素
        elem_li.setAttribute("id", i);
        elem_li.innerHTML = "<a href = '#' >" + i + "</a>"; // 设置元素的内容
        elem_li.onclick = function () {
            currentPage = $(this).find("a").eq(0).text();
            showProductTable(currentPage, conType, conName);
        }
        pages.insertBefore(elem_li, document.getElementById("next")); // 添加到UL中去
    }
    prePage = totalPage;
    /*上一页和下一页*/
    var pre = document.getElementById("previous");
    var nex = document.getElementById("next");
    pre.onmouseover = function () {
        if (currentPage <= 1) {
            this.setAttribute("class", "disabled");
        } else {
            this.removeAttribute("class", "disabled");
        }
    }
    nex.onmouseover = function () {
        if (currentPage >= totalPage) {
            this.setAttribute("class", "disabled");
        } else {
            this.removeAttribute("class", "disabled");
        }
    }
    pre.onclick = function () {
        if (currentPage <= 1) {
            currentPage = 1;
        } else {
            currentPage = currentPage - 1;
        }
        showProductTable(currentPage, conType, conName);
    }
    nex.onclick = function () {
        if (currentPage >= totalPage) {
            currentPage = totalPage;
        } else {
            currentPage = currentPage + 1;
        }
        showProductTable(currentPage, conType, conName);
    }

}

/*产品修改*/
function product_edit(proQR) {
    var data = window.btoa(encodeURIComponent("productQR=" + proQR));
    window.location.href = "product-edit.html?" + data;
}

/*产品-删除*/
function product_del(obj, id, kind) {
    if (kind == 0) {
        if (confirm('确认要删除这条记录吗？')) {
            $.ajax({
                url: "/productInfo!delProduct",
                type: "post",
                data: {
                    "productQR": id
                },
                success: function (data) {
                    $(obj).parents("tr").remove();
                    alert("删除成功!");
                    window.location.href = "product-list.html";
                },
                error: function (data) {
                },
            });
        }
    }
    if (kind == 1) {
        $.ajax({
            url: "/productInfo!delProduct",
            type: "post",
            data: {
                "productQR": id
            },
            success: function (data) {
                $(obj).parents("tr").remove();
                window.location.href = "product-list.html";
            },
            error: function (data) {
            },
        });
    }
}
function brokenTotal() {
    $.ajax({
        url:"/productInfo!brokenTotal" , // 请求路径
        type:"POST" , //请求方式
        success:function (data) {
            document.getElementById("brokenCount").innerText = data;
        },//响应成功后的回调函数
    });
}

/*显示商品列表*/
function showProductTable(currentPage, conType, conName) {
    document.getElementById("firstCb").checked = false;
    var data = "currentPage=" + currentPage + "&rows=4&conType=" + conType + "&conName=" + conName + "";
    var url = "/productInfo!showByPage";
    sendRequest("post", url, data, ShowAllBack);
    /*   $.ajax({
           url:"/productInfo!showByPage" , // 请求路径
           type:"POST" , //请求方式
           data:{"currentPage":1,"rows":3},
           success:function (data) {
               alert(data);
               json = JSON.parse(data);
               alert("aaa"+json);
           },//响应成功后的回调函数
           error:function () {
               alert("出错啦...")
           },//表示如果请求响应出现错误，会执行的回调函数
           dataType:"json"//设置接受到的响应数据的格式
       });*/
}

function ShowAllBack(result) {
    var result = JSON.parse(result);
    json = JSON.parse(result);
    document.getElementById("totalCount").innerText = json.totalCount;
    for (var i = 1; i <= prePage; i++) {    //清除之前的页码
        $("#" + i + "").remove();
    }
    if(json.list.length == 0){
        document.getElementById("tbody").innerHTML =  "<tr><td valign=\"top\" colspan=\"14\" >没有数据</td></tr>";
    }else{
        show(json.list, json.currentPage, json.totalPage);
    }

}

function conProduct() {
    var conType = document.getElementById("conType").value;
    var conName = document.getElementById("conName").value;
    showProductTable(1, conType, conName);
}




